home *** CD-ROM | disk | FTP | other *** search
/ Mac Magazin/MacEasy 79 / maccd 79.iso / multimedial / GL Tron / Source / gltron / screenshot.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-05-26  |  2.6 KB  |  120 lines  |  [TEXT/CWIE]

  1. #include "gltron.h"
  2.  
  3. #include <png.h>
  4.  
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7.  
  8. int screenShot(char *filename, gDisplay *d);
  9. int bmpScreenShot(char *filename, gDisplay *d);
  10.  
  11. void doScreenShot() {
  12.   char buf[100];
  13.  
  14. #ifndef macintosh  
  15.   sprintf(buf, "gltron-0.59-%d.png", screenshots);
  16.   screenShot(buf, game->screen);
  17.   screenshots++;
  18.  
  19. #endif
  20. }
  21.  
  22. void doBmpScreenShot() {
  23.   char buf[100];
  24.  
  25.   sprintf(buf, "gltron-0.59-%d.bmp", screenshots);
  26.   bmpScreenShot(buf, game->screen);
  27.   screenshots++;
  28. }
  29.  
  30. FILE *fp;
  31.  
  32. void user_write_data(png_structp png_ptr,
  33.              png_bytep data, png_size_t length) {
  34.   fwrite(data, length, 1, fp);
  35. }
  36.  
  37. void user_flush_data(png_structp png_ptr) {
  38.   fflush(fp);
  39. }
  40.  
  41. int screenShot(char *filename, gDisplay *d) {
  42.   unsigned char *data;
  43.  
  44.   png_structp png_ptr;
  45.   png_infop info_ptr;
  46.   png_byte **row_pointers;
  47.   int colortype;
  48.   int width, height;
  49.   int i;
  50.  
  51.   width = d->w;
  52.   height = d->h;
  53.   data = malloc(width * height * 3);
  54.   
  55.   glReadPixels(0, 0, width, height, GL_RGB, GL_UNSIGNED_BYTE, data);
  56.   
  57.   fp = fopen(filename, "wb");
  58.   if(!fp) {
  59.     fprintf(stderr, "can't open %s for writing\n", filename);
  60.     return 1;
  61.   }
  62.   
  63.   png_ptr = png_create_write_struct
  64.     /*     (PNG_LIBPNG_VER_STRING, (png_voidp)user_error_ptr,
  65.        user_error_fn, user_warning_fn); */
  66.     (PNG_LIBPNG_VER_STRING, 0, 0, 0);
  67.  
  68.   if (!png_ptr)
  69.     return 1;
  70.  
  71.   info_ptr = png_create_info_struct(png_ptr);
  72.   if (!info_ptr) {
  73.       png_destroy_write_struct(&png_ptr, (png_infopp)NULL);
  74.       return 1;
  75.   }
  76.   /* png_init_io(png_ptr, fp); */
  77.   png_set_write_fn(png_ptr, 0, user_write_data, user_flush_data);
  78.  
  79.   colortype = PNG_COLOR_TYPE_RGB;
  80.  
  81.   png_set_IHDR(png_ptr, info_ptr, width, height,
  82.            8, colortype, PNG_INTERLACE_NONE,
  83.            PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
  84.   png_write_info(png_ptr, info_ptr);
  85.  
  86.   /* get pointers */
  87.   row_pointers = (png_byte**) malloc(height * sizeof(png_byte*));
  88.   for(i = 0; i < height; i++)
  89.     row_pointers[i] = data + (height - i - 1) 
  90.       * 3 * width;
  91.  
  92.   png_write_image(png_ptr, row_pointers);
  93.   png_write_end(png_ptr, info_ptr);
  94.   png_destroy_write_struct(&png_ptr, &info_ptr);
  95.   
  96.   free(row_pointers);
  97.   free(data);
  98.  
  99.   fprintf(stderr, "written screenshot to %s\n", filename);
  100.   return 0;
  101. }
  102.  
  103.  
  104. int bmpScreenShot(char *filename, gDisplay *d) {
  105.   unsigned char *data;
  106.   int width, height;
  107.     
  108.   width = d->w;
  109.   height = d->h;
  110.   data = malloc(width * height * 3);
  111.   glReadPixels(0, 0, width, height, GL_RGB, GL_UNSIGNED_BYTE, data);
  112.   SystemWriteBMP(filename, width, height, data);
  113.   free(data);
  114.   fprintf(stderr, "written screenshot to %s\n", filename);
  115.   return 0;
  116. }
  117.  
  118.  
  119.  
  120.